package com.cy.personer.web;

import com.cy.personer.bean.Combo;
import com.cy.personer.bean.Desk;
import com.cy.personer.bean.Item;
import com.cy.personer.bean.TreeNode;
import com.cy.personer.biz.ComboBiz;
import com.cy.personer.biz.DeskBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.cy.personer.util.BarcodeUtil.generateFile;

@Controller
@RequestMapping("/desk")
public class DeskHandler {

    @Resource
    DeskBiz deskBizImpl;

    /**
     * 查询科室列表
     * @author 林少艺
     */
    @RequestMapping(value = "/getDesk.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody List<Desk> getDesks(Desk desk){
        List<Desk> desks = deskBizImpl.getDesks(desk);
        return desks;
    }

    /**
     * 查询科室列表(树形专用)
     * @author 林少艺
     */
    @RequestMapping(value = "/getDeskTree.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody List<TreeNode> getDeskTree(Desk desk){
        List<TreeNode> list = new ArrayList<TreeNode>();
        List<Desk> desks = deskBizImpl.getDesks(desk);
        for (Desk desk1 : desks) {
            list.add(new TreeNode("000"+desk1.getDeskId(),"0",desk1.getDeskName(),false,false,false));
            for (Item item : desk1.getItems()) {
                list.add(new TreeNode(""+item.getItemId(),"000"+desk1.getDeskId(),item.getItemName(),false,false,false));
            }
        }
        return list;
    }

    /**
     * 查询科室
     * @author whl
     */
    @RequestMapping(value = "desk.action")
    public  String Alldesk(HttpServletRequest request, Integer pageNum){
        if (pageNum == null||pageNum==0) {
            pageNum = 1;
        }
        PageInfo<Desk> deskList =deskBizImpl.findDesk(pageNum);
            request.setAttribute("deskList",deskList);
            return "admin/desk";
        }


    /**
     * 科室增加
     * @author whl
     */
    @RequestMapping(value = ("add.action"),method=RequestMethod.POST,produces = "application/json;charset=utf-8")
    public @ResponseBody Boolean add(Desk desk){
        boolean addesk =deskBizImpl.add(desk);
        if (addesk){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 科室删除
     * @author whl
     */
    @RequestMapping(value = ("del.action"),method = RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody Boolean del(int deskId){
        boolean deldesk=deskBizImpl.del(deskId);
        if (deldesk){
            return true;
        }else {
            return  false;
        }
    }
    /**
     * 科室修改
     * @author whl
     */
    @RequestMapping(value = ("update.action"),method = RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody Boolean up(Desk desk){
        boolean updesk=deskBizImpl.update(desk);
        if (updesk){
            return true;
        }else {
            return  false;
        }
    }


}
